package com.ruoyi.coupon.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CouponDelayConfig {
    /*
    * 1、声明正常交换机(order_exchange)和正常队列(order_queue)，并绑定
    * */
    @Bean
    public Exchange couponExchange() {
        return
            ExchangeBuilder.topicExchange("coupon_exchange").durable(true).build();
    }
    @Bean
    public Queue couponQueue() {
    /*
    * 3、正常队列绑定死信交换机
    * */
        return QueueBuilder.durable("coupon_queue")
            // 正常队列绑定死信交换机: deadLetterExchange 死信交换机名称deadLetterRoutingKey 死信交换机路由key
            .deadLetterExchange("coupon_dlx_exchange").deadLetterRoutingKey("coupon.dlx.cancel")
            // 4、设置队列的过期时间 30min
            .ttl(10 * 1000)
            .build();
    }
    @Bean
    public Binding bindingCouponNormal() {
        return
                BindingBuilder.bind(couponQueue()).to(couponExchange()).with("coupon.#").noargs();
    }
    /*
     * 2、声明死信交换机(order_dlx_exchange)和死信队列(order_dlx_queue)，并绑定
     * */
    @Bean
    public Exchange couponDlxExchange() {
        return
                ExchangeBuilder.topicExchange("coupon_dlx_exchange").durable(true).build();
    }
    @Bean
    public Queue couponDlxQueue() {
        return QueueBuilder.durable("coupon_dlx_queue").build();
    }
    @Bean
    public Binding bindingCouponDlx() {
        return
                BindingBuilder.bind(couponDlxQueue()).to(couponDlxExchange()).with("coupon.dlx.#").noargs();
    }
}